﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2017/2/4
 * 时间: 21:57
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Net;
using System.Text.RegularExpressions;

namespace MiniHttpServer
{
	/// <summary>
	/// Description of HttpFilter.
	/// </summary>
	public class HttpFilter
	{
		Server server;
		HttpListenerRequest request;
		HttpListenerResponse response;
		byte[] responseContent;

		public byte[] ResponseContent {
			get {
				return responseContent;
			}
		}
		
		public HttpFilter(Server server, 
		                  HttpListenerRequest request, 
		                  HttpListenerResponse response)
		{
			this.server = server;
			this.request = request;
			this.response = response;
			responseContent = null;
		}
		
		public bool Filter()
		{
			string rawUrl = request.RawUrl;
			string urlFile = HttpFile.GetUrlFile(rawUrl);
			string extFname = HttpFile.GetUrlFileExtName(urlFile);
			System.Collections.Specialized.NameValueCollection urlParams = 
				request.QueryString;
			
			return FilterByExtFname(urlFile, extFname);
		}
		
		bool FilterByExtFname(string urlFile, string extFname)
		{
			if (extFname == ".html" || 
			    extFname == ".htm" || 
			    extFname == ".js" || 
			    extFname == ".css" || 
			    extFname == ".txt"
			   )
			{
				response.ContentType = "text/html; charset=UTF-8";
				return FilterStaticFile(urlFile);
			}
			else if (extFname == ".jpg" || 
			         extFname == ".jpeg" || 
			         extFname == ".png" || 
			         extFname == ".bmp" || 
			         extFname == ".gif"
			        )
			{
				if (extFname == ".jpg" || extFname == ".jpeg")
				{
					response.ContentType = "image/pjpeg";
				}
				else if (extFname == ".png")
				{
					response.ContentType = "image/x-png";
				}
				else if (extFname == ".bmp")
				{
					response.ContentType = "image/bmp";
				}
				else if (extFname == ".gif")
				{
					response.ContentType = "image/gif";
				}
				return FilterStaticFile(urlFile);
			}
			else if (extFname == ".asp" || 
			         extFname == ".aspx" || 
			         extFname == ".php" || 
			         extFname == ".jsp"
			        )
			{
				response.ContentType = "text/html; charset=UTF-8";
				return FilterDynamicFile(urlFile);
			}
			else if (extFname == ".exe" || 
			         extFname == ".rar" || 
			         extFname == ".zip" || 
			         extFname == ".asp"
			        )
			{
				return FilterStaticFile(urlFile);
			}
			else
			{
				return FilterStaticFile(urlFile);
			}
		}
		
		bool FilterStaticFile(string urlFile)
		{
			string filePath = HttpFile.GetFilePath(server.WwwRootPath, urlFile);
			byte[] fileContent = HttpFile.GetFileContent(filePath);
			if (fileContent == null)
			{
				Logger.WriteError("获取文件内容出错: {0}", filePath);
				return false;
			}
			
			response.ContentLength64 = fileContent.Length;
			
			responseContent = fileContent;
			return true;
		}
		
		bool FilterDynamicFile(string urlFile)
		{
			string filePath = HttpFile.GetFilePath(server.WwwRootPath, urlFile);
			byte[] fileContent = HttpFile.GetFileContent(filePath);
			if (fileContent == null)
			{
				Logger.WriteError("获取文件内容出错: {0}", filePath);
				return false;
			}
			
			string s = fileContent.ToString();
			var regex = new Regex("<%.+%>");
			s = regex.Replace(s, "");
			
			response.ContentLength64 = fileContent.Length;
			
			responseContent = fileContent;
			return true;
		}
	}
}
